from flask import Blueprint,jsonify,request
from conf.comm import sqlalchemy_context
# 密码加密
from werkzeug.security import generate_password_hash,check_password_hash
from database import *
import json
import re
#蓝图初始化
bp_users = Blueprint('users',__name__)
#注册功能实现
@bp_users.route('/reg/',methods=['POST'])
@sqlalchemy_context()
def reg():
    data = request.data
    if not data:
        data = request.form
    else:
        # 获取用户输入
        data = json.loads(request.data)
    
    # 正则验证
    reg = re.match('^1[3-9]\d{9}$',data['mobile'])
    if not reg:
        return jsonify({"code":10012,"mes":'手机号不合法'})
    try:
        user = User(username=data['username'],password=generate_password_hash(data['password']),mobile=data['mobile'])
        db.session.add(user)
        db.session.commit()
        return jsonify({'code':200})
    except:
        return jsonify({'code':10010})